package ssau.knyazev.db.core;


import java.sql.*;

import ssau.knyazev.common.UniException;
import ssau.knyazev.db.common.FrameworkSettings;



public class ConnectionHelper {

	private static Connection res = null;
    
	public static Connection getConnection() throws UniException{
		try {
			if (res == null){
				Class.forName("oracle.jdbc.driver.OracleDriver");
			}
            res = DriverManager.getConnection(FrameworkSettings.DB_URL, FrameworkSettings.DB_USER, FrameworkSettings.DB_PASSWORD);
		} catch (ClassNotFoundException e) {
			throw new UniException("JDBC driver is not found", e);
        } catch (SQLException e) {
			throw new UniException("Couldn't get connection", e);
		}
		return res;
	}
	
	
	public static void closeStatement(Statement state){
		try {
			state.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void closeConnection(Connection con, Statement state){
		try {
			if (state != null){
				state.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
